package com.wenwei.sharebox.activity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.orhanobut.logger.Logger;
import com.wenwei.sharebox.R;
import com.wenwei.sharebox.base.BaseActivity;
import com.wenwei.sharebox.base.Constant;
import com.wenwei.sharebox.bean.BannerBean;
import com.wenwei.sharebox.bean.GoodsDetailsBean;
import com.wenwei.sharebox.net.ApiManager;
import com.wenwei.sharebox.net.BaseSubscriber;
import com.wenwei.sharebox.utils.CheckPermissionUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bingoogolapple.bgabanner.BGABanner;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * Created by Administrator on 2017/7/7.
 */
public class GoodsDetailsActivity extends BaseActivity implements BGABanner.Adapter, EasyPermissions.PermissionCallbacks {
    @Bind(R.id.home_page_banner)
    BGABanner homePageBanner;
    @Bind(R.id.base_title)
    TextView baseTitle;
    @Bind(R.id.details_name)
    TextView detailsName;
    @Bind(R.id.details_price)
    TextView detailsPrice;
    @Bind(R.id.msg_1)
    TextView msgTv_1;
    @Bind(R.id.msg_2)
    TextView msgTv_2;

    @Override
    public int setLayoutResourceId() {
        return R.layout.activity_goods_details;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    protected void initView() {
        super.initView();
        baseTitle.setText("商品详情");
    }

    @Override
    public void fillBannerItem(BGABanner banner, View itemView, Object model, int position) {
        try{
            GoodsDetailsBean.ShopImagesBean bannerBean = (GoodsDetailsBean.ShopImagesBean) model;
            Glide.with(mContext).load(bannerBean.getShopUrl()).into((ImageView) itemView);
        }catch (Exception e){
            Logger.d(e.toString());
        }
    }

    @OnClick({R.id.location_rl, R.id.base_back, R.id.sao_tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.location_rl:
//                cameraTask();
                goActivity(mContext, MapLocationActivity.class);
                break;
            case R.id.base_back:
                finish();
                break;
            case R.id.sao_tv:
//                initPermission();

                Bundle checkBundle = new Bundle();
                checkBundle.putInt("qr_type", Constant.QR_TYPE_GOODS);
                goActivity(mContext, QRcodeActivity.class,checkBundle);
                break;
        }
    }

    @AfterPermissionGranted(99)
    public void cameraTask() {
        if (EasyPermissions.hasPermissions(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
            // Have permission, do the thing!
            Toast.makeText(this, "TODO: Camera things", Toast.LENGTH_LONG).show();
            goActivity(mContext, MapLocationActivity.class);
        } else {
            // Ask for one permission
            EasyPermissions.requestPermissions(this, "定位权限",
                    99, Manifest.permission.ACCESS_FINE_LOCATION);
        }
    }



    /**
     * 初始化权限事件
     */
    private void initPermission() {
        //检查权限
        String[] permissions = CheckPermissionUtils.checkPermission(this);
        if (permissions.length == 0) {
            //权限都申请了
            //是否登录
//            goActivity(mContext,InputCodeActivity.class);
            Bundle checkBundle = new Bundle();
            checkBundle.putInt("qr_type", Constant.QR_TYPE_GOODS);
            goActivity(mContext, QRcodeActivity.class,checkBundle);
        } else {
            //申请权限

            ActivityCompat.requestPermissions(this, permissions, 100);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        // Forward results to EasyPermissions
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {

    }

    /**
     * 请求CAMERA权限码
     */
    public static final int REQUEST_CAMERA_PERM = 101;

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
            new AppSettingsDialog.Builder(this, "当前App需要申请camera权限,需要打开设置页面么?")
                    .setTitle("权限申请")
                    .setPositiveButton("确认")
                    .setNegativeButton("取消", null /* click listener */)
                    .setRequestCode(99)
                    .build()
                    .show();
        }
    }


    @Override
    protected void initData() {
        super.initData();
        getGoodsDetails();
    }

    private BaseSubscriber<GoodsDetailsBean> detailsSubscriber;

    private void getGoodsDetails() {
        detailsSubscriber = new BaseSubscriber<GoodsDetailsBean>(mContext) {
            @Override
            public void onSuccess(GoodsDetailsBean goodsDetailsBean) {
                homePageBanner.setAdapter(new BGABanner.Adapter() {
                    @Override
                    public void fillBannerItem(BGABanner banner, View itemView, Object model, int position) {
                        try{
                            GoodsDetailsBean.ShopImagesBean bannerBean = (GoodsDetailsBean.ShopImagesBean) model;
                            Glide.with(mContext).load(bannerBean.getShopUrl()).error(R.drawable.banner).into((ImageView) itemView);
                        }catch (Exception e){
                            Logger.d(e.toString());
                        }

                    }
                });
                homePageBanner.setDelegate(new BGABanner.Delegate() {
                    @Override
                    public void onBannerItemClick(BGABanner banner, View itemView, Object model, int position) {

                    }
                });
                homePageBanner.setData(goodsDetailsBean.getShopImages(), null);

                detailsName.setText(goodsDetailsBean.getShopName());
                detailsPrice.setText("￥"+goodsDetailsBean.getMoney());
                msgTv_1.setText("租金："+goodsDetailsBean.getIntegralNum()+"积分/小时 押金：￥"+goodsDetailsBean.getDeposit());
                msgTv_2.setText("购买可得"+goodsDetailsBean.getGetIntegral()+"积分");
            }
        };
        ApiManager.getInstance().getGoodsDetails(detailsSubscriber, getIntent().getIntExtra("goodsId", 1));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
